#include "registerwindow.h"
#include "ui_registerwindow.h"
#include "mainwindow.h"
#include <QSqlQuery>
#include <QMessageBox>

RegisterWindow::RegisterWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::RegisterWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("注册界面");

    ui->IdlineEdit->installEventFilter(this);
    ui->passwordlineEdit->installEventFilter(this);

    ui->myWidget->hide();
}

RegisterWindow::~RegisterWindow()
{
    delete ui;
}

bool RegisterWindow::eventFilter(QObject *watched, QEvent *event)
{
    if ( (watched == ui->IdlineEdit) && (event->type() == QEvent::MouseButtonPress) )
    {
        callKeyBoard();
        ui->IdlineEdit->setFocus();
    }
    else if ( (watched == ui->passwordlineEdit) && (event->type() == QEvent::MouseButtonPress) )
    {
        callKeyBoard();
        ui->passwordlineEdit->setFocus();
    }

    return QMainWindow::eventFilter(watched,event);
}

void RegisterWindow::callKeyBoard()
{
    ui->myWidget->show();
}

void RegisterWindow::on_loginButton_clicked()
{
    //将数据插入到数据库---在多界面编程中，数据库连接只需要在一个界面上进行，其他界面是直接访问数据库
    QSqlQuery query;

    QString sqlInsert = QString("insert into staff(name,password) values('%1','%2');")
                        .arg(ui->IdlineEdit->text())
                        .arg(ui->passwordlineEdit->text());

    if(query.exec(sqlInsert))
    {
        QMessageBox::information(this,"注册提示","注册成功");
    }
    ui->IdlineEdit->clear();
    ui->passwordlineEdit->clear();
}

void RegisterWindow::on_loginButton_2_clicked()
{
    MainWindow *mw = new MainWindow;
    mw->show();
    this->close();
}

void RegisterWindow::on_minButton_clicked()
{
    ui->myWidget->hide();
}
